package com.bw.ods;

import com.alibaba.ververica.cdc.connectors.mysql.MySQLSource;
import com.alibaba.ververica.cdc.connectors.mysql.table.StartupOptions;
import com.alibaba.ververica.cdc.debezium.DebeziumSourceFunction;
import com.alibaba.ververica.cdc.debezium.StringDebeziumDeserializationSchema;
import com.bw.MyKafkaUtil;
import com.bw.fun.MyDebeziumDeserializationSchema;
import org.apache.flink.core.fs.Path;
import org.apache.flink.runtime.state.StateBackend;
import org.apache.flink.runtime.state.filesystem.FsStateBackend;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.CheckpointConfig;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class Test1 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env=StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        System.setProperty("HADOOP_USER_NAME", "hadoop");
        env.enableCheckpointing(5000L);
//        StateBackend stateBackend=new FsStateBackend("file:///D:\\javaProject\\FlinkMonth2203A\\ck1");
        FsStateBackend stateBackend = new FsStateBackend(new Path("hdfs://hadoop102:8020/yk8/ck").toUri(), 1024 * 1024);
        env.setStateBackend(stateBackend);
//         保存检查检查点数据
        env.getCheckpointConfig().enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);


        // CDC采集数据
        // sudo vim /etc/my.cnf
        // sudo systemctl restart mysqld
        DebeziumSourceFunction<String> mysqlSource= MySQLSource.<String>builder()
                .hostname("hadoop102")
                .port(3306)
                .username("root")
                .password("123456")
                .databaseList("fast_food")
                .startupOptions(StartupOptions.initial())  //全量
//                .startupOptions(StartupOptions.latest())   //增量
                .deserializer(new MyDebeziumDeserializationSchema())
                .build();

        // 加入到flink source
        DataStreamSource<String> mysqlDS=env.addSource(mysqlSource);

        mysqlDS.print();
//        mysqlDS.addSink(MyKafkaUtil.getKafkaProducer("ods_base_topic_yk8"));
        env.execute();

    }

}
